﻿using StockMatch.Service.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Titan;

namespace StockMatch.Service
{
   public  class AdminUserService : ContextBoundObject
    {

        #region 增删改

        public bool Insert(AdminUser adminUser)
        {
            try
            {
                using (var cn = Environment.OpenSession())
                {
                    if (IsExistUserName(adminUser.AdminUserName, 0))
                    {
                        throw new StockException("此用户名已存在");
                    }
                    return cn.Insert(adminUser) > 0;
                }
            }
            catch (Exception ex)
            {
                throw new StockException(ex.Message);
            }
        }

        public bool Update(AdminUser adminUser, PropertyExpression[] updateProperty)
        {
            try
            {
                using (var cn = Environment.OpenSession())
                {
                    if (IsExistUserName(adminUser.AdminUserName, adminUser.AdminUserId))
                    {
                        throw new StockException("此用户名已存在");
                    }
                    return cn.Update(adminUser, updateProperty) > 0;
                }
            }
            catch (Exception ex)
            {
                throw new StockException(ex);
            }
        }

        public bool Delete(int userId)
        {
            try
            {
                using (var cn = Environment.OpenSession())
                {
                    var adminUser = new AdminUser() { AdminUserId = userId };
                    return cn.Delete(adminUser) > 0;
                }
            }
            catch (Exception ex)
            {
                throw new StockException(ex.Message);
            }
        }

        #endregion


        #region 查询

        public AdminUser GetUser(int adminUserId)
        {
            try
            {
                using (var cn = Environment.OpenSession())
                {
                    var adminUser = new AdminUser() { AdminUserId = adminUserId };
                    if (cn.Select(adminUser))
                    {
                        return adminUser;
                    }
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw new StockException(ex.Message);
            }
        }

        /// <summary>
        /// 根据用户名查询
        /// </summary>
        /// <param name="adminUserName"></param>
        /// <returns></returns>
        public bool IsExistUserName(string adminUserName, int userId)
        {
            try
            {
                using (var cn = Environment.OpenSession())
                {
                    var query = new QueryExpression();
                    query.EntityType = typeof (AdminUser);
                    query.Select(AdminUser_.AdminUserId);
                    query.Wheres.Add(AdminUser_.AdminUserName.TEqual(adminUserName));
                    if (userId > 0)
                    {
                        query.Wheres.Add(AdminUser_.AdminUserId.TNotEqual(userId));
                    }
                    var adminUsers = new AdminUsers();
                    cn.SelectCollection(adminUsers.Items, query);
                    return adminUsers.Items.Count > 0;
                }
            }
            catch (Exception ex)
            {
                throw new StockException(ex.Message);
            }
        }

        public AdminUsers GetUsers(QueryExpression query)
        {
            try
            {
                using (var cn = Environment.OpenSession())
                {
                    AdminUsers adminUsers = new AdminUsers();
                    adminUsers.TotalCount = cn.SelectCollection(adminUsers.Items, query);
                    return adminUsers;
                }
            }
            catch (Exception ex)
            {
                throw new StockException(ex.Message);
            }
        }

        #endregion
    }
}
